package com.example.allapp;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	TextView tv_app;
	TextView cfg;
	ArrayList<AppInfo> mAppInfos;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 获取DPI和分辨率
		cfg = (TextView) findViewById(R.id.cfg);
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		Locale locale = getResources().getConfiguration().locale;
		// 获取语言简写，比如zh-CN
		String language = locale.getLanguage() + "-" + locale.getCountry();
		// 国家语言 中文简体
		String displayLanguage = locale.getDisplayName();
		cfg.setText("dpi:" + metric.densityDpi + "\n分辨率:" + metric.widthPixels
				+ "*" + metric.heightPixels + "\n语言简写:" + language + "\n国家语言:"
				+ displayLanguage + "\n");
		// 加载哪个分辨率的图片
		ImageView iv = (ImageView) findViewById(R.id.iv);
		//iv.setImageResource(R.drawable.image);
		
		
		
		// 所有app包名和类名
		tv_app = (TextView) findViewById(R.id.tv_app);
		mAppInfos = getAppInfo(getApplicationContext());
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < mAppInfos.size(); i++) {
			AppInfo appInfo = mAppInfos.get(i);

			// sb.append(appInfo.getPackageName() + "    " +
			// appInfo.getAppName()
			// + "\n");

			sb.append("应用名:" + appInfo.getAppName() + "\n包名:"
					+ appInfo.getPackageName() + "\n类名:"
					+ appInfo.getClassName() + "\n\n");
		}
		Log.d("ludaxu", sb.toString());
		System.out.println(sb.toString());
		tv_app.setText(sb.toString());

	}

	private ArrayList<AppInfo> getAppInfo(Context mContext) {
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		List<ResolveInfo> mApps = getPackageManager().queryIntentActivities(
				mainIntent, 0);
		ArrayList<AppInfo> mAppInfos = new ArrayList<AppInfo>();

		int mSize = mApps.size();
		for (int i = mSize - 1; i >= 0; i--) {
			ResolveInfo info = mApps.get(i);
			String name = (String) info.loadLabel(mContext.getPackageManager());
			Drawable icon = info.loadIcon(mContext.getPackageManager());
			if (name == null) {
				continue;
			} else {
				AppInfo mAppInfo = new AppInfo();
				mAppInfo.setAppName(name);
				mAppInfo.setAppIcon(icon);
				mAppInfo.setClassName(info.activityInfo.name);
				mAppInfo.setPackageName(info.activityInfo.packageName);
				mAppInfos.add(mAppInfo);
			}
		}
		return mAppInfos;

	}
}
